/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.utils;

import java.awt.Container;
import java.io.File;
import java.sql.Connection;
import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.jpa.JpaEntityManager;

/**
 *
 * @author REDDYS
 */


public class ReportUtils {
   static EntityManager em;
   Connection con = null;
   public Connection getConnection(){
       em = EntityManagerImpl.getEntityManager();
       UnitOfWorkImpl uow = (UnitOfWorkImpl)((JpaEntityManager)em).getActiveSession().acquireUnitOfWork(); 
       Connection connection = uow.getAccessor().getConnection();
       return connection;
   }
    
  public void callReport(String reportName,HashMap hm){

      JasperPrint jasperPrint = generateReport(reportName,hm);
        JRViewer viewer = new JRViewer(jasperPrint);
        JFrame jframe = new JFrame();
        jframe.getContentPane().add(viewer);
        jframe.pack();
        jframe.setVisible(true);
     //   JasperViewer.viewReport (jasperPrint);
  }
    
     public JasperPrint generateReport(String reportName,HashMap hm) {
        try {
            if (con == null) {
                try {
                    con = getConnection();
 
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            JasperPrint jasperPrint = null;
            if (hm == null) {
                hm = new HashMap();
            }
            try {
                /**You can also test this line if you want to display 
                 * report from any absolute path other than the project root path*/
                //jasperPrint = JasperFillManager.fillReport("F:/testreport/"+reportName+".jasper",hm, con);
                jasperPrint = JasperFillManager.fillReport(reportName, hm, con);
                      System.out.println("jasperPrint--->"+jasperPrint);
            } catch (JRException e) {
                e.printStackTrace();
            }
            return jasperPrint;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
 
 
    }
 
}
